using AutoMapper;
using ERP.HumanResources.HumanResourceManagement.Dtos;
using ERP.MasterData.Dtos;
using ERP.RepositoryManage.Dtos;
using ERP.ERPModels.Production.MasterData;
using ERP.RBAC;
using ERP.RBAC.Dto;
using System.Collections.Generic;
using ERP.ERPModels.HumanResources.MasterData;
using ERP.ERPModels.EquipmentManage;
using ERP.EquipmentManage.Dtos;
using ERP.ERPModels.RepositoryManage;
using ERP.ERPModels.Production.ProductionManagement;
using ERP.ERPModels.WorkforceManage;
using ERP.WorkforceManage.Dtos;
using ERP.ERPModels.QualityAssurance;
using ERP.QualityAssurance.Dto;

namespace ERP;

public class ERPApplicationAutoMapperProfile : Profile
{
    public ERPApplicationAutoMapperProfile()
    {
        /* You can configure your AutoMapper mapping configuration here.
         * Alternatively, you can split your mapping configurations
         * into multiple profile classes for a better organization. */
         
        // 排班计划映射配置
        CreateMap<SchedulingPlan, SchedulingPlanDto>();
        CreateMap<CreateSchedulingPlanDto, SchedulingPlan>();
        
        // 节假日设置映射配置
        CreateMap<HolidaySettingModel, HolidaySettingDto>();
        CreateMap<CreateOrUpdateHolidaySettingDto, HolidaySettingModel>();
         
        // 职级管理映射配置
        CreateMap<PositionLevel, PositionLevelDto>();
        CreateMap<CreatePositionLevelDto, PositionLevel>();
        CreateMap<UpdatePositionLevelDto, PositionLevel>();

        // 物料管理
        CreateMap<Material, MaterialDto>();
        CreateMap<CreateMaterialDto, Material>();
        CreateMap<UpdateMaterialDto, Material>();

        // 客户管理映射配置
        CreateMap<Customers, CustomerManagementDto>();
        CreateMap<CustomerManagementDto, Customers>();
        
        // 采购入库管理
        CreateMap<Procurement, ProcurementDto>();
        CreateMap<CreateProcurementDto, Procurement>();
        CreateMap<UpdateProcurementDto, Procurement>();
        CreateMap<ProcurementDetail, ProcurementDetailDto>();
        CreateMap<CreateProcurementDetailDto, ProcurementDetail>();

        // 供应商选择
        CreateMap<Supplier, SupplierSelectDto>();

        // 物料选择
        CreateMap<Material, MaterialSelectDto>();

        // 物料分类管理
        CreateMap<MaterialClass, MaterialClassDto>()
            .ForMember(dest => dest.IsEnabled, opt => opt.MapFrom(src => src.Status == 1));
        CreateMap<CreateMaterialClassDto, MaterialClass>()
            .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.IsEnabled ? 1 : 0))
            .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(src => System.DateTime.Now));
        CreateMap<UpdateMaterialClassDto, MaterialClass>()
            .ForMember(dest => dest.Status, opt => opt.MapFrom(src => src.IsEnabled ? 1 : 0));

        // 车间管理映射配置
        CreateMap<Workshop, WorkshopDto>();
        CreateMap<CreateWorkshopDto, Workshop>();
        CreateMap<UpdateWorkshopDto, Workshop>();

        // 角色管理映射配置
        CreateMap<Roles, RolePerDto>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id))
            .ForMember(dest => dest.Rolename, opt => opt.MapFrom(src => src.Rolename));
        CreateMap<RolePerDto, Roles>();

        CreateMap<AssignRolesDto, UserRole>().ReverseMap();

        CreateMap<ProcessConfigurationUpdateDTO, ProcessConfiguration>().ReverseMap();
        
        // 设备点检保养项目映射配置
        CreateMap<Dvsubject, DvsubjectDto>();
        CreateMap<CreateDvsubjectDto, Dvsubject>();
        CreateMap<UpdateDvsubjectDto, Dvsubject>();

        // 装箱管理映射配置
        CreateMap<Packing, PackingDto>();
        CreateMap<CreatePackingDto, Packing>();
        CreateMap<UpdatePackingDto, Packing>();
        CreateMap<PackingDetail, PackingDetailDto>()
            .ForMember(dest => dest.Remark, opt => opt.MapFrom(src => src.Remark ?? src.Desc));
        CreateMap<CreatePackingDetailDto, PackingDetail>()
            .ForMember(dest => dest.Num, opt => opt.MapFrom(src => (int)src.ActualQuantity))
            .ForMember(dest => dest.Desc, opt => opt.MapFrom(src => src.Remark));
        CreateMap<UpdatePackingDetailDto, PackingDetail>()
            .ForMember(dest => dest.Num, opt => opt.MapFrom(src => (int)src.ActualQuantity))
            .ForMember(dest => dest.Desc, opt => opt.MapFrom(src => src.Remark));
        
        // 生产工单选择映射
        CreateMap<ProductionWorkOrders, WorkOrderSelectDto>()
            .ForMember(dest => dest.WorkOrderCode, opt => opt.MapFrom(src => src.OrderCode))
            .ForMember(dest => dest.WorkOrderName, opt => opt.MapFrom(src => src.OrderName))
            .ForMember(dest => dest.OrderNumber, opt => opt.MapFrom(src => src.OrderCode))
            .ForMember(dest => dest.CustomerCode, opt => opt.MapFrom(src => string.Empty))
            .ForMember(dest => dest.CustomerName, opt => opt.MapFrom(src => string.Empty));
        
        // 客户选择映射
        CreateMap<Customers, CustomerSelectDto>();

        // 设备维护计划映射配置
        CreateMap<EquipmentMaintenancePlan, EquipmentMaintenancePlanDto>();
        CreateMap<CreateEquipmentMaintenancePlanDto, EquipmentMaintenancePlan>();
        CreateMap<UpdateEquipmentMaintenancePlanDto, EquipmentMaintenancePlan>();

        // 设备维护计划与项目关联映射配置
        CreateMap<EquipmentMaintenancePlanDvsubject, DvsubjectDto>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.DvsubjectId))
            .ForMember(dest => dest.Subcode, opt => opt.Ignore())
            .ForMember(dest => dest.Subname, opt => opt.Ignore())
            .ForMember(dest => dest.Subtype, opt => opt.Ignore())
            .ForMember(dest => dest.Isdel, opt => opt.Ignore())
            .ForMember(dest => dest.Subcontext, opt => opt.Ignore())
            .ForMember(dest => dest.Subarea, opt => opt.Ignore());

        // 设备管理请求DTO映射配置
        CreateMap<GetDvsubjectInputDto, GetDvsubjectInputDto>(); // 自映射
        CreateMap<DeleteDvsubjectRequestDto, DeleteDvsubjectRequestDto>(); // 自映射
        CreateMap<AddDvsubjectToPlanRequestDto, AddDvsubjectToPlanRequestDto>(); // 自映射
        CreateMap<SavePlanDvsubjectsRequestDto, SavePlanDvsubjectsRequestDto>(); // 自映射
    }
}
